#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(){
  int fd, bytes;
  unsigned char data[3];

  //set mouse device location
  const char *mouseDevice = "/dev/input/mice";

  // Open Mouse as Read Only
  fd = open(mouseDevice, O_RDONLY);
  if(fd == -1){
    printf("ERROR Opening %s\n", mouseDevice);
    printf("You may not have permission to read from this device.\n");
    return -1;
  }

  int l, m, r;
  signed char x, y;
  while(1){
    // Read Mouse     
    bytes = read(fd, data, sizeof(data));

    if(bytes > 0){
      l = data[0] & 0x1;
      r = data[0] & 0x2;
      m = data[0] & 0x4;

      x = data[1];
      y = data[2];
      /*action if left mouse button is clicked
      if(l == 1){
        printf("Left Mouse Button Clicked.\n");
      }
      */
      printf("x=%d, y=%d, l=%d, m=%d, r=%d\n", x, y, l, m, r);

    }   
  }
  return 0; 
}